# -*- coding=utf-8
import logging
import os
import sys

import requests
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client

# 1. 设置用户属性, 包括 secret_id, secret_key, region等。Appid 已在 CosConfig 中移除，请在参数 Bucket 中带上 Appid。Bucket 由 BucketName-Appid 组成
secret_id = 'AKIDyyFnIm7NYzLdq1ACINNBCNRQKDB3t6Xz'  # 用户的 SecretId，建议使用子账号密钥，授权遵循最小权限指引，降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
secret_key = 'lxHgE7ydAoOrPSWes7LUbp3JXj5d9bu8'  # 用户的 SecretKey，建议使用子账号密钥，授权遵循最小权限指引，降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
bucket = 'context-1257245862'

# secret_id = 'AKIDoY8VgQZ3we9RceATIwPfidOyMbeVr5pO'     # 用户的 SecretId，建议使用子账号密钥，授权遵循最小权限指引，降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
# secret_key = 'TkqmZDgpqfVr8HF1aw9AyiGwVs57xa5R'  # 用户的 SecretKey，建议使用子账号密钥，授权遵循最小权限指引，降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
# bucket = 'demo-1251808174'

region = 'ap-chengdu'  # 替换为用户的 region，已创建桶归属的 region 可以在控制台查看，https://console.cloud.tencent.com/cos5/bucket
# COS 支持的所有 region 列表参见 https://cloud.tencent.com/document/product/436/6224
# token = '6jmwo5E6ibUgBjQZRajnwmNNv2HW59ba88615ed1bca62a27320efa548b14320bEcSKHQlM_7imF128QIzcxC_bggVEw9t8d4zl6tbk8YgNBv1--wwHm6BVQp3B1ziAHSdXtdzJp9cqZU3XjK44PX1fd1e7j3CbCEdQKCfvVUMj6uFxhCaWYLFUC0ZwTvTcm8-4zGie1l2F5Q9V5Aepb4RRIf12kg-KD6mtgA8AaWOBnjREvoxhv1mzgX3LtZ2GPxa8Dn6o_LnKmLwUUA-6M57FqBh4kt3rxNtTbkJExYRHCxhJNTG9tXxwnw2m9AI6JVQsKiEPZILW8-KqmOIdr3VSHtpRIywoOOMfsLHK-Y5V1kSlFuh3hivP7Y_Nnj44OrhatuPTI0Pf7nBZh_1B4dBNrMR2JNLA4AbhDMikpqaCRPm9nJB5HjUjU6fK82XrLKUr_dm9cBAdvko141oo_c8y-o1F0JdBCplwUSynh-4FHE9VZIdBJxB08WHmHBkWeuBU5YFwC48Q1kH7j4kOd8h2OTtH63EA9dEwwclO4rkzUW7TOUGzQhy_QL4oiyf3ZltOP7alj1mND9zrEGvUGIQdFxj5uzUQelMfHoLUi5HvMGhCpU2mnGY4OJnKbJE-nSC8jsboOpA_5DG4o8XDa6FfuLKyKiV1YcSGuLpx33WET0XRR4dOUPQQYrQ-O_dC3TARB-ohPHJZnvYyjt4XAFJwuqM_M2TjFM5plTVVBb-Op9Ylin0WlwcAPWjDXkRdQ_i6EBlHb6l7vOtmFx_wehU_THaScUc1waHC8Gr7OZs'               # 如果使用永久密钥不需要填入 token，如果使用临时密钥需要填入，临时密钥生成和使用指引参见 https://cloud.tencent.com/document/product/436/14048
token = None
scheme = 'https'  # 指定使用 http/https 协议来访问 COS，默认为 https，可不填


# 正常情况日志级别使用 INFO，需要定位时可以修改为 DEBUG，此时 SDK 会打印和服务端的通信信息
logging.basicConfig(level=logging.INFO, stream=sys.stdout)

config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)
client = CosS3Client(config)

from datetime import date

# 获取当前日期
today = date.today()

# 将日期转换为字符串，格式为 "YYYY-MM"
current_year_month = today.strftime("%Y-%m")
filename = 'test1.pdf'
print(current_year_month)
# 生成上传 URL，未限制请求头部和请求参数
url = client.get_presigned_url(
    Method='PUT',
    Bucket=bucket,
    Key=f'media/tmp/edoc/{current_year_month}/{filename}',
    Expired=120  # 120秒后过期，过期时间请根据自身场景定义
)
print(url)

# https://examplebucket-1250000000.cos.ap-chengdu.myqcloud.com/exampleobject?q-sign-algorithm=sha1&q-ak=AKIDyyFnIm7NYzLdq1ACINNBCNRQKDB3t6Xz&q-sign-time=1722994872%3B1722995052&q-key-time=1722994872%3B1722995052&q-header-list=host&q-url-param-list=&q-signature=ae0f2b2c9cbe395730400a23b99fbe1c6a1bd85a
# url = 'https://examplebucket-1250000000.cos.ap-chengdu.myqcloud.com/exampleobject?q-sign-algorithm=sha1&q-ak=AKIDyyFnIm7NYzLdq1ACINNBCNRQKDB3t6Xz&q-sign-time=1722994872%3B1722995052&q-key-time=1722994872%3B1722995052&q-header-list=host&q-url-param-list=&q-signature=ae0f2b2c9cbe395730400a23b99fbe1c6a1bd85a'
# # # 使用上传 URL
# response = requests.put(url=url, data=b'123')
# print(response.content)
